/// <reference types="vite/client" />

import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { ElMessage } from 'element-plus'
  
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      meta: { 
        requiresAuth: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue'),
      meta: {
        title: '登录',
        i18n: 'common.login'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register.vue'),
      meta: {
        title: '注册'
      }
    },
    {
      path: '/user',
      name: 'user',
      redirect: '/user/profile',
      meta: {
        title: '用户',
        i18n: 'common.user'
      },
      children: [
        {
          path: 'profile',
          name: 'profile',
          component: () => import('../views/user/Profile.vue'),
          meta: { 
            requiresAuth: true,
            title: '个人信息',
            i18n: 'common.profile'
          }
        }
      ]
    },
    {
      path: '/subject',
      name: 'subject',
      component: () => import('../views/subject/SubjectView.vue'),
      meta: { 
        requiresAuth: true,
        title: '学科管理',
        i18n: 'common.subjectManagement'
      }
    },
    {
      path: '/graph',
      name: 'graph',
      component: () => import('../views/graph/GraphView.vue'),
      meta: { 
        requiresAuth: true,
        title: '知识图谱',
        i18n: 'common.knowledgeGraph'
      }
    },
    {
      path: '/question',
      name: 'question',
      component: () => import('../views/question/QuestionView.vue'),
      meta: { 
        requiresAuth: true,
        title: '题库管理',
        i18n: 'common.questionManagement'
      }
    },
    {
      path: '/question/detail',
      name: 'questionDetail',
      component: () => import('../views/question/Detail.vue'),
      meta: { 
        requiresAuth: true,
        title: '题目详情',
        parent: 'question',
        i18n: 'common.questionDetail'
      }
    },
    {
      path: '/paper',
      name: 'paper',
      redirect: '/paper/list',
      meta: {
        title: '试卷管理',
        i18n: 'common.paperManagement'
      },
      children: [
        {
          path: 'list',
          name: 'paperList',
          component: () => import('../views/paper/PaperList.vue'),
          meta: { 
            requiresAuth: true,
            title: '试卷列表',
            i18n: 'paper.list'
          }
        },
        {
          path: 'create',
          name: 'paperCreate',
          component: () => import('../views/paper/PaperCreate.vue'),
          meta: { 
            requiresAuth: true,
            title: '创建试卷',
            i18n: 'paper.create'
          }
        }
      ]
    },
    {
      path: '/create',
      name: 'create',
      redirect: '/paper/create',
      meta: {
        title: '创建试卷',
        i18n: 'paper.create'
      },
      children: [
        {
          path: 'manual',
          name: 'manual',
          component: () => import('../views/paper/create/ManualCreate.vue'),
          meta: { 
            requiresAuth: true,
            title: '手动创建',
            i18n: 'paper.manual'
          }
        },
        {
          path: 'rule',
          name: 'rule',
          component: () => import('../views/paper/create/RuleCreate.vue'),
          meta: { 
            requiresAuth: true,
            title: '规则创建',
            i18n: 'paper.rule'
          }
        }
      ]
    },
    {
      path: '/statistics',
      name: 'statistics',
      component: () => import('../views/statistics/StatisticsView.vue'),
      meta: { 
        requiresAuth: true,
        title: '统计分析',
        i18n: 'common.statisticsAnalysis'
      }
    },
    {
      path: '/help',
      name: 'help',
      component: () => import('../views/HelpView.vue'),
      meta: { 
        requiresAuth: true,
        title: '使用文档',
        i18n: 'common.useDocument'
      }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  if (to.meta.requiresAuth && !isAuthenticated) {
    // 需要登录但未登录，重定向到登录页
    ElMessage.error('请先登录')
    next({ name: 'login' })
  } else if (isAuthenticated && (to.name === 'login' || to.name === 'register')) {
    // 已登录用户访问登录/注册页，重定向到首页
    next({ name: 'home' })
  } else {
    next()
  }
})

export default router
